package integration

import (
	"context"
	commpb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/hotel/api"
	hpb "gitee.com/micro-services/protocol/hotel/server"
)

func CreateHotel(ctx context.Context, r *pb.APICreateHotelReq) (*pb.APICreateHotelRsp, error) {
	rsp, err := GetProxyManger().HotelClientProxy.CreateHotel(ctx, &hpb.CreateHotelReq{
		HotelName:        r.HotelName,
		HotelTitle:       r.HotelTitle,
		Description:      r.Description,
		HotelPhone:       r.HotelPhone,
		HotelAddress:     r.HotelAddress,
		HotelImg:         r.HotelImg,
		DetailImg:        r.DetailImg,
		Longitude:        r.Longitude,
		Latitude:         r.Latitude,
		AreaID:           r.AreaID,
		BusinessCircleID: r.BusinessCircleID,
		HotelType:        r.HotelType,
		Score:            r.Score,
	})

	if err != nil {
		return nil, err
	}

	return &pb.APICreateHotelRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func DeleteHotel(ctx context.Context, r *pb.APIDeleteHotelReq) (*pb.APIDeleteHotelRsp, error) {
	rsp, err := GetProxyManger().HotelClientProxy.DeleteHotel(ctx, &hpb.DeleteHotelReq{
		HotelID: r.HotelID,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIDeleteHotelRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func UpdateHotel(ctx context.Context, r *pb.APIUpdateHotelReq) (*pb.APIUpdateHotelRsp, error) {
	rsp, err := GetProxyManger().HotelClientProxy.UpdateHotel(ctx, &hpb.UpdateHotelReq{
		HotelID:     r.HotelID,
		HotelTitle:  r.HotelTitle,
		Description: r.Description,
		HotelPhone:  r.HotelPhone,
		HotelImg:    r.HotelImg,
		DetailImg:   r.DetailImg,
		HotelType:   r.HotelType,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIUpdateHotelRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func BatchHotelInfo(ctx context.Context, r *pb.APIBatchHotelInfoReq) ([]*commpb.HotelInfo, error) {
	rsp, err := GetProxyManger().HotelClientProxy.BatchHotelInfo(ctx, &hpb.BatchHotelInfoReq{
		HotelID: r.HotelID,
	})
	if err != nil {
		return nil, err
	}
	hotelInfo := make([]*commpb.HotelInfo, 0)
	if rsp.HotelInfo != nil {
		for _, v := range rsp.HotelInfo {
			hotelInfo = append(hotelInfo, &commpb.HotelInfo{
				HotelID:          v.HotelID,
				HotelName:        v.HotelName,
				HotelTitle:       v.HotelTitle,
				Description:      v.Description,
				HotelPhone:       v.HotelPhone,
				HotelAddress:     v.HotelAddress,
				HotelImg:         v.HotelImg,
				DetailImg:        v.DetailImg,
				Longitude:        v.Longitude,
				Latitude:         v.Latitude,
				AreaID:           v.AreaID,
				Area:             v.Area,
				BusinessCircleID: v.BusinessCircleID,
				BusinessCircle:   v.BusinessCircle,
				HotelType:        v.HotelType,
				Score:            v.Score,
			})
		}
	}

	return hotelInfo, nil
}

func SearchHotel(ctx context.Context, r *pb.APISearchHotelReq) ([]*commpb.HotelInfo, int64, error) {
	rsp, err := GetProxyManger().HotelClientProxy.SearchHotel(ctx, &hpb.SearchHotelReq{
		HotelName:        r.HotelName,
		HotelTitle:       r.HotelTitle,
		HotelPhone:       r.HotelPhone,
		HotelAddress:     r.HotelAddress,
		AreaID:           r.AreaID,
		BusinessCircleID: r.BusinessCircleID,
		HotelType:        r.HotelType,
		Score:            r.Score,
		Offset:           r.Offset,
		Limit:            r.Limit,
	})
	if err != nil {
		return nil, 0, err
	}

	hotelInfo := make([]*commpb.HotelInfo, 0)
	if rsp.HotelInfo != nil {
		for _, v := range rsp.HotelInfo {
			hotelInfo = append(hotelInfo, &commpb.HotelInfo{
				HotelID:          v.HotelID,
				HotelName:        v.HotelName,
				HotelTitle:       v.HotelTitle,
				Description:      v.Description,
				HotelPhone:       v.HotelPhone,
				HotelAddress:     v.HotelAddress,
				HotelImg:         v.HotelImg,
				DetailImg:        v.DetailImg,
				Longitude:        v.Longitude,
				Latitude:         v.Latitude,
				AreaID:           v.AreaID,
				Area:             v.Area,
				BusinessCircleID: v.BusinessCircleID,
				BusinessCircle:   v.BusinessCircle,
				HotelType:        v.HotelType,
				Score:            v.Score,
			})
		}
	}
	return hotelInfo, rsp.Count, nil
}

func Recommend(ctx context.Context, r *pb.APIRecommendReq) ([]*commpb.HotelInfo, int64, error) {
	rsp, err := GetProxyManger().HotelClientProxy.Recommend(ctx, &hpb.RecommendReq{
		UserLongitude: r.UserLongitude,
		UserLatitude:  r.UserLatitude,
		Offset:        r.Offset,
		Limit:         r.Limit,
	})
	if err != nil {
		return nil, 0, err
	}

	hotelInfo := make([]*commpb.HotelInfo, 0)
	if rsp.HotelInfo != nil {
		for _, v := range rsp.HotelInfo {
			hotelInfo = append(hotelInfo, &commpb.HotelInfo{
				HotelID:          v.HotelID,
				HotelName:        v.HotelName,
				HotelTitle:       v.HotelTitle,
				Description:      v.Description,
				HotelPhone:       v.HotelPhone,
				HotelAddress:     v.HotelAddress,
				HotelImg:         v.HotelImg,
				DetailImg:        v.DetailImg,
				Longitude:        v.Longitude,
				Latitude:         v.Latitude,
				AreaID:           v.AreaID,
				Area:             v.Area,
				BusinessCircleID: v.BusinessCircleID,
				BusinessCircle:   v.BusinessCircle,
				HotelType:        v.HotelType,
				Score:            v.Score,
			})
		}
	}
	return hotelInfo, rsp.Count, nil
}
